package com.ljkj.cordial.chat.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.ModifyPhoneView;
import com.ljkj.cordial.chat.widget.DownTimeTextView;
import com.nic.view.util.JToast;
import com.nic.view.util.ValidateUtils;
import com.nic.view.widget.ContainsEmojiEditText;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 添加修改手机号
 */
public class ModifyPhoneActivity extends AppEventActivity<ModifyPhoneView> {


    @BindView(R.id.et_captcha)
    ContainsEmojiEditText etCaptcha;
    @BindView(R.id.tv_captcha)
    DownTimeTextView tvCaptcha;
    @BindView(R.id.et_phone)
    ContainsEmojiEditText etPhone;
    @BindView(R.id.ll_original_phone)
    LinearLayout llOriginalPhone;
    @BindView(R.id.et_captcha1)
    ContainsEmojiEditText etCaptcha1;
    @BindView(R.id.tv_captcha1)
    DownTimeTextView tvCaptcha1;
    @BindView(R.id.tv_phone_title)
    TextView tvPhoneTitle;
    @BindView(R.id.et_phone1)
    ContainsEmojiEditText etPhone1;
    private int type;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_modify_phone;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        type = getIntent().getIntExtra(Canstants.KEY_TYPE, Canstants.TYPE_ADD);
        String phone = getIntent().getStringExtra(Canstants.KEY_PHONE);
        if (Canstants.TYPE_ADD == type || TextUtils.isEmpty(phone)) {
            getToolbar().setTitle("添加手机号");
            tvPhoneTitle.setText("手机号");
            llOriginalPhone.setVisibility(View.GONE);
        } else if (Canstants.TYPE_MODIFY == type) {
            getToolbar().setTitle("修改手机号");
            etPhone.setText(phone);
            etPhone.setFocusable(false);
            etPhone.setFocusableInTouchMode(false);
            etPhone.setClickable(false);
        }
        getToolbarRightTv().setVisibility(View.VISIBLE);
        getToolbarRightTv().setText("保存");
        getToolbarRightTv().setOnClickListener(v -> {

            if (TextUtils.isEmpty(etPhone1.getText())) {
                etPhone1.setError("不能为空");
                return;
            } else if (!ValidateUtils.Mobile(etPhone1.getText().toString())) {
                etPhone1.setError("不是有效号码");
                return;
            } else if (TextUtils.isEmpty(etCaptcha1.getText())) {
                etCaptcha1.setError("不能为空");
                return;
            }


            if (Canstants.TYPE_ADD == type) {
                obtainView().doSetupPhone(etPhone1.getText().toString(), etCaptcha1.getText().toString());
            } else if (Canstants.TYPE_MODIFY == type) {
                if (TextUtils.isEmpty(etPhone.getText())) {
                    etPhone.setError("不能为空");
                    return;
                } else if (!ValidateUtils.Mobile(etPhone.getText().toString())) {
                    etPhone.setError("不是有效号码");
                    return;
                } else if (TextUtils.isEmpty(etCaptcha.getText())) {
                    etCaptcha.setError("不能为空");
                    return;
                }
                obtainView().doModifyPhone(etPhone.getText().toString(), etCaptcha.getText().toString(), etPhone1.getText().toString(), etCaptcha1.getText().toString());
            }

        });
        tvCaptcha.setOnCaptchaListener(v -> {
            if (TextUtils.isEmpty(etPhone.getText())) {
                etPhone.setError("不能为空");
                return false;
            } else if (!ValidateUtils.Mobile(etPhone.getText().toString())) {
                etPhone.setError("不是有效号码");
                return false;
            }
            obtainView().doGetVerificationCode(etPhone.getText().toString());
            return true;
        });
        tvCaptcha1.setOnCaptchaListener(v -> {
            if (TextUtils.isEmpty(etPhone1.getText())) {
                etPhone1.setError("不能为空");
                return false;
            } else if (!ValidateUtils.Mobile(etPhone1.getText().toString())) {
                etPhone1.setError("不是有效号码");
                return false;
            }
            obtainView().doGetVerificationCode(etPhone1.getText().toString());
            return true;
        });
    }

    @Override
    protected ModifyPhoneView bindNettyMessageView() {
        return new ModifyPhoneView(new ModifyPhoneView.ModifyPhoneCallback() {
            @Override
            public void onVerifyCodeCallback(int code, String msg, String phone) {
                if (IMSContacts.ResponseCode.SUCCESS != code) {
                    if (!TextUtils.isEmpty(phone)) {
                        if (phone.equals(etPhone.getText().toString())) {
                            tvCaptcha.reset();
                        } else if (phone.equals(etPhone1.getText().toString())) {
                            tvCaptcha1.reset();
                        }
                    }
                    JToast.showShort(ModifyPhoneActivity.this, msg);
                    return;
                }
                if (!TextUtils.isEmpty(phone)) {
                    if (phone.equals(etPhone.getText().toString())) {
                        JToast.showShort(ModifyPhoneActivity.this, "原手机验证码发送成功！");
                    } else if (phone.equals(etPhone1.getText().toString())) {
                        JToast.showShort(ModifyPhoneActivity.this, "新手机验证码发送成功！");
                    }
                }
            }

            @Override
            public void onSetupPhoneCallback(int code, String msg, UserDto data) {
                if (IMSContacts.ResponseCode.SUCCESS != code) {
                    JToast.showShort(ModifyPhoneActivity.this, msg);
                    return;
                }
                UserTableBean u = UserTableBean.getCurrentUser();
                u.setPhone(etPhone1.getText().toString());
                DbController.getInstance().getWrite().getUserTableBeanDao().update(u);

                Intent intent = new Intent();
                intent.putExtra(Canstants.KEY_TITLE, u.getPhone());
                setResult(Canstants.RESULT_CODE_300, intent);
                finish();
            }

            @Override
            public void onModifyPhoneCallback(int code, String msg, UserDto data) {
                if (IMSContacts.ResponseCode.SUCCESS != code) {
                    JToast.showShort(ModifyPhoneActivity.this, msg);
                    return;
                }
                UserTableBean u = UserTableBean.getCurrentUser();
                u.setPhone(etPhone1.getText().toString());
                DbController.getInstance().getWrite().getUserTableBeanDao().update(u);

                Intent intent = new Intent();
                intent.putExtra(Canstants.KEY_TITLE, u.getPhone());
                setResult(Canstants.RESULT_CODE_300, intent);
                finish();
            }
        });
    }

    public static void jumpTo(Activity activity, int type, String phone, int requestCode) {
        Intent intent = new Intent(activity, ModifyPhoneActivity.class);
        intent.putExtra(Canstants.KEY_TYPE, type);
        intent.putExtra(Canstants.KEY_PHONE, phone);
        activity.startActivityForResult(intent, requestCode);
    }
}
